package front.end.subestacao.ui;

import java.awt.Insets;
import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.geom.Point2D;
import java.io.IOException;

import javax.swing.JInternalFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

public class JInternalFrameMod extends JInternalFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Insets insets;

	  private int top = 0, bottom = 0, topRow = 0, bottomRow = 0;
	  
	  private FrontEndSubView viewPrinc;

	public JInternalFrameMod(FrontEndSubView viewPrinc) {
		this.viewPrinc = viewPrinc;
		DragSource dragSource = DragSource.getDefaultDragSource();
		dragSource.createDefaultDragGestureRecognizer(this,
				DnDConstants.ACTION_COPY_OR_MOVE,
				new JInternalModDragGestureListener());
		DropTarget dropTarget = new DropTarget(this,
				new JInternalModDropTargetListener());
	}

	private static class JInternalModDragGestureListener implements
			DragGestureListener {
		public void dragGestureRecognized(DragGestureEvent dragGestureEvent) {

			
		}
	}

	private class JInternalModDropTargetListener implements DropTargetListener {

		public void dragEnter(DropTargetDragEvent dropTargetDragEvent) {
			
			 // Setup positioning info for auto-scrolling
		      top = Math.abs(getLocation().y);
		      bottom = top + getParent().getHeight();
		      // topRow = getClosestRowForLocation(0, top);
		      // bottomRow = getClosestRowForLocation(0, bottom);
		      insets = new Insets(top + 10, 0, bottom - 10, getWidth());

		}

		public void dragExit(DropTargetEvent dropTargetEvent) {
		}

		public void dragOver(DropTargetDragEvent dropTargetDragEvent) {
		}

		public void dropActionChanged(DropTargetDragEvent dropTargetDragEvent) {
		}

		public synchronized void drop(DropTargetDropEvent dropTargetDropEvent) {
			 Point2D local = dropTargetDropEvent.getLocation();
			 
			 //System.out.println(local);
			 viewPrinc.adicionarComponente(local);
			 
			 
			/*
			 System.out.println("Adicionando 2 !!!!");
			 Transferable tr = dropTargetDropEvent.getTransferable();
			 
			 try {
				Object userObject = tr.getTransferData(TransferableTreeNode.DEFAULT_MUTABLE_TREENODE_FLAVOR);
				
			 } catch (UnsupportedFlavorException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 
			 dropTargetDropEvent.dropComplete(true);
			
			*/
			
			
		    
		     

		}
	}
}
